//
//  LongFormViewController.m
//  LongForm
//
//  Created by User on 10/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "LongFormViewController.h"

@implementation LongFormViewController

@synthesize scviewLong;

- (void)dealloc
{
    [txtCurrentField release];

    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    //---set the viewable frame of the scroll view---
    scviewLong.frame = CGRectMake(0, 0, 320, 460);
    
    //---set the content size of the scroll view---
    [scviewLong setContentSize:CGSizeMake(320, 710)];
    
    [super viewDidLoad];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void) viewWillAppear:(BOOL)animated
{
    //---registers the notifications for keyboard---
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardDidShow:) 
                                         name:UIKeyboardDidShowNotification 
                                         object:self.view.window]; 
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(keyboardDidHide:)
                                          name:UIKeyboardDidHideNotification
                                          object:nil];
}

-(void) viewWillDisappear:(BOOL)animated {
    //---removes the notifications for keyboard---
    [[NSNotificationCenter defaultCenter]      
        removeObserver:self 
        name:UIKeyboardWillShowNotification 
        object:nil];
    [[NSNotificationCenter defaultCenter] 
        removeObserver:self 
        name:UIKeyboardWillHideNotification 
        object:nil];
}

//---when a TextField view begins editing---
-(void) textFieldDidBeginEditing:(UITextField *)textFieldView {  
    txtCurrentField = textFieldView;
}


-(BOOL) textFieldShouldReturn:(UITextField *) textFieldView {  
    [textFieldView resignFirstResponder];
    return NO;
}

//---when a TextField view is done editing---
-(void) textFieldDidEndEditing:(UITextField *) textFieldView {  
    txtCurrentField = nil;
}

//---when the keyboard appears---
-(void) keyboardDidShow:(NSNotification *) notification {
    
    if (flagKeyboardIsShown) return;
    
    NSDictionary* info = [notification userInfo];
    
    //---obtain the size of the keyboard---
    NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;
    
    //---resize the scroll view (with keyboard)---
    CGRect viewFrame = [scviewLong frame];
    viewFrame.size.height -= keyboardSize.height;
    scviewLong.frame = viewFrame;
    
    //---scroll to the current text field---
    CGRect textFieldRect = [txtCurrentField frame];
    [scviewLong scrollRectToVisible:textFieldRect animated:YES];
    
    flagKeyboardIsShown = YES;
}


//---when the keyboard disappears---
-(void) keyboardDidHide:(NSNotification *) notification {
    NSDictionary* info = [notification userInfo];
    
    //---obtain the size of the keyboard---
    NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;
    
    //---resize the scroll view back to the original size (without keyboard)---
    CGRect viewFrame = [scviewLong frame];
    viewFrame.size.height += keyboardSize.height;
    scviewLong.frame = viewFrame;
    
    flagKeyboardIsShown = NO;
}



@end
